﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.DataModel;
using System.Data.DataControl;

namespace WebAppWithDataEnum.Configuration
{
    public partial class ModelCreatorForm : System.Web.UI.Page
    {
        private List<PropertyControls> propertySets;
        protected void Page_Load(object sender, EventArgs e)
        {
            this.propertySets = this.Session["propertySets"] == null ? new List<PropertyControls>() : (List<PropertyControls>)this.Session["propertySets"];
            this.Session["propertySets"] = null;

            this.propertySets.FindAll(delegate(PropertyControls property)
            {

                this.SetPropertyControls(property);
                return true;
            });
        }

        protected void btnAddProertySet_Click(object sender, EventArgs e)
        {
            PropertyControls property = new PropertyControls(this.propertySets.Count);
            this.SetPropertyControls(property);
            this.propertySets.Add(property);

            this.Session["propertySets"] = this.propertySets;
        }

        private void SetPropertyControls(PropertyControls property)
        {
            Literal ltrName = new Literal();
            ltrName.Text = "PropertyName";
            Literal ltrType = new Literal();
            ltrType.Text = "Type";
            Literal ltrSize = new Literal();
            ltrSize.Text = "Size";
            Literal ltrBr = new Literal();
            ltrBr.Text = "<br />";

            this.form1.Controls.Add(ltrName);
            this.form1.Controls.Add(property.NameTextBox);
            this.form1.Controls.Add(ltrType);
            this.form1.Controls.Add(property.TypeDropDownList);
            this.form1.Controls.Add(ltrSize);
            this.form1.Controls.Add(property.SizeTextBox);
            this.form1.Controls.Add(property.PrimaryCheckBox);
            this.form1.Controls.Add(ltrBr);
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //接続文字列

            string assemblyName = MapPath("~/").Split('\\')[MapPath("~/").Split('\\').Length - 2];

            string className = this.txtClassName.Text;

            //プロパティを設定
            string[] properties = new string[this.propertySets.Count];
            for (int i = 0; i < this.propertySets.Count; i++)
            {
                properties[i] = "Name=" + this.propertySets[i].NameTextBox.Text +
                                ";Type=" + this.propertySets[i].TypeDropDownList.Text +
                                ";Size=" + (this.propertySets[i].SizeTextBox.Text.Trim().Length == 0 ? "0" : this.propertySets[i].SizeTextBox.Text.Trim()) +
                                ";Primary=" + this.propertySets[i].PrimaryCheckBox.Checked.ToString();
            }

            //モデルクラス生成
            ModelCreator creator = new ModelCreator(assemblyName, className, properties, this.chkTableDrop.Checked);
            creator.SetClassText();
            if (this.chkCreateTable.Checked)
            {
                DataConnection.DbConnectionFactory();
                DataConnection.Connection.Open();
                creator.SetDatabaseObject(MapPath("~/"));
                DataConnection.Connection.Close();

            }

            creator.CreateFile(MapPath("~/"));

            Response.Redirect("~/Configuration/ModelCreatorForm.aspx");

        }
    }
}
